package com.huawei.hms.aaid;

import android.app.Activity;
import android.content.Context;
import com.huawei.hms.aaid.b.e;
import com.huawei.hms.aaid.c.a;
import com.huawei.hms.aaid.c.b;
import com.huawei.hms.aaid.c.c;
import com.huawei.hms.aaid.constant.AaidIdConstant;
import com.huawei.hms.aaid.entity.TokenResult;
import com.huawei.hms.api.Api;
import com.huawei.hms.api.Api.ApiOptions.NoOptions;
import com.huawei.hms.api.HuaweiApiAvailability;
import com.huawei.hms.common.ApiException;
import com.huawei.hms.common.HuaweiApi;
import com.huawei.hms.common.internal.Preconditions;
import com.huawei.hms.core.aidl.IMessageEntity;
import com.huawei.hms.utils.JsonUtil;
import o.fjo;
import o.fjx;

public class HmsInstanceIdEx {
    public static final String TAG = HmsInstanceIdEx.class.getSimpleName();
    private static final byte[] a = new byte[0];
    private Context b = null;
    private c c = null;
    private HuaweiApi<NoOptions> d;

    private HmsInstanceIdEx(Context context) {
        this.b = context;
        this.c = new c(context, "aaid");
        Api api = new Api(HuaweiApiAvailability.HMS_API_NAME_PUSH);
        if (context instanceof Activity) {
            this.d = new HuaweiApi((Activity) context, api, null, new e());
        } else {
            this.d = new HuaweiApi(context, api, null, new e());
        }
        this.d.setKitSdkVersion(30003301);
    }

    public static HmsInstanceIdEx getInstance(Context context) {
        Preconditions.checkNotNull(context);
        return new HmsInstanceIdEx(context);
    }

    public fjo<TokenResult> getToken() {
        fjx o_fjx;
        String a = b.a(this.b, AaidIdConstant.getToken);
        try {
            IMessageEntity a2 = a.a(null, null, this.b);
            a2.setAaid(HmsInstanceId.getInstance(this.b).getId());
            return this.d.doWrite(new com.huawei.hms.aaid.b.c(AaidIdConstant.getToken, JsonUtil.createJsonString(a2), this.b, a));
        } catch (RuntimeException e) {
            o_fjx = new fjx();
            o_fjx.ॱ(com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR));
            b.a(this.b, AaidIdConstant.getToken, a, com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
            return o_fjx.ॱ();
        } catch (Exception e2) {
            o_fjx = new fjx();
            o_fjx.ॱ(com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR));
            b.a(this.b, AaidIdConstant.getToken, a, com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
            return o_fjx.ॱ();
        }
    }

    public String getAAId(String str) throws ApiException {
        if (str == null) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_ARGUMENTS_INVALID);
        }
        try {
            if (this.c.c(str)) {
                return this.c.a(str);
            }
            String a = a.a(str + a.a(this.b));
            this.c.a(str, a);
            this.c.a(a(str), Long.valueOf(System.currentTimeMillis()));
            return a;
        } catch (RuntimeException e) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        } catch (Exception e2) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        }
    }

    public long getCreationTime(String str) throws ApiException {
        if (str == null) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_ARGUMENTS_INVALID);
        }
        try {
            if (!this.c.c(a(str))) {
                getAAId(str);
            }
            return this.c.b(a(str));
        } catch (RuntimeException e) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        } catch (Exception e2) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        }
    }

    public void deleteAAID(String str) throws ApiException {
        if (str == null) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_ARGUMENTS_INVALID);
        }
        try {
            if (this.c.c(str)) {
                this.c.d(str);
                this.c.d(a(str));
            }
        } catch (RuntimeException e) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        } catch (Exception e2) {
            throw com.huawei.hms.aaid.constant.a.a(com.huawei.hms.aaid.constant.a.ERROR_INTERNAL_ERROR);
        }
    }

    private String a(String str) {
        return AaidIdConstant.CREATE_TIME + str;
    }
}
